import { getQueryVariable } from '@/utils/publicUtils';
import { reportLog } from '@/utils/reportLog';
// import store from '@/store'

export default {
	created(el, binding) {
		const { value } = binding;
		let currentRid = getQueryVariable('rid');
		// 用户有按钮权限：给按钮增加监听事件, 上报日志
		el.addEventListener('click', () => {
			// 点击按钮，上报日志
			reportLog({
				type: 4,
				resourceKey: value.permis.toString(),
				pageKey: currentRid,
			});
		});
	},
	beforeMount(el, binding) {
		const urlParams = new URLSearchParams(window.location.search);
		const pagetypeValue = urlParams.get('pageType');
		const { value } = binding;
		let list = JSON.parse(localStorage.getItem('portalResourceList'));
		if (process.env.NODE_ENV !== 'development') {
			let flg = true;
			for (var i = 0; i < value.permis.length; i++) {
				if (list.indexOf(value.permis[i]) == -1) {
					flg = false;
					break;
				}
			}

			if (value.businessFlag) {
				if (pagetypeValue != 'edit' || !flg) {
					console.log('未找到的permis', value.permis);
					el.disabled = true;
					el.style = 'cursor: no-drop';
				}
			} else {
				if (!flg) {
					el.disabled = true;
					el.style = 'cursor: no-drop';
				}
			}

			if (value.isHiden && !flg) {
				el.parentNode && el.parentNode.removeChild(el);
			}
		}
	},
};
